package ext;

import java.util.*;

/**
 * 完美世界笔试题
 *
 * 最长递增序列
 */
public class WanmeiTestUtil {

    //存放以第i个元素结尾的最长递增序列长度
    int[] lis;
    //记录以第i个元素结尾的LIS的前一个位置
    int[] pre;
    //记录最长LIS
    int maxLen = 1;
    //记录最长LIS的最后一个元素位置
    int maxIdx = 0;
    //原来的数组
    int[] nums;

    public WanmeiTestUtil(int[] nums) {
        lis = new int[nums.length];
        pre = new int[nums.length];
        this.nums = nums;
        cal(nums, nums.length - 1);
    }

    //计算以end-1位置为尾的LIS
    public int cal(int[] nums, int end) {
        if (end == 0) return 1;

        int localMax = 1;   //以end-1位置元素为尾的LIS长度
        int preIdx = -1;    //以end-1位置元素为尾的前一个元素索引
        for (int i = 0; i < end; ++i) {
            int subMax;
            if (lis[i] != 0) subMax = lis[i];
            else {
                subMax = cal(nums, i);
                lis[i] = subMax;
            }

            if (nums[end] > nums[i] && subMax + 1 > localMax) {
                localMax = subMax + 1;
                preIdx = i;
            }
        }
        lis[end] = localMax;
        pre[end] = preIdx;

        if (localMax > maxLen) {
            maxLen = localMax;
            maxIdx = end;
        }

        return localMax;
    }

    //根据pre得到最长递增序列
    public int[] getLis() {
        int[] res = new int[maxLen];
        int idx = maxIdx;
        for (int i = maxLen; i > 0; --i) {
            res[i - 1] = nums[idx];
            idx = pre[idx];
        }
        return res;
    }

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            int[] nums = convert(line);

            if (nums == null || nums.length == 0) {
                System.out.println();
                continue;
            }

            WanmeiTestUtil sol = new WanmeiTestUtil(nums);
            int[] res = sol.getLis();

            //输出结果
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < res.length; ++i) {
                if (i != 0)
                    sb.append(" ");
                sb.append(res[i]);
            }
            //sb.append(" ");
            //sb.append(sol.maxLen);
            System.out.println(sb.toString());
        }

    }

    //将一行数据转换成数组
    public static int[] convert(String line) {
        String[] words = line.split(" ");

        ArrayList<Integer> list = new ArrayList<Integer>();
        for (String word : words) {
            try {
                Integer value = Integer.valueOf(word);
                list.add(value);
            } catch (NumberFormatException e) {

            }
        }

        int[] nums = new int[list.size()];
        for (int i = 0; i < nums.length; ++i)
            nums[i] = list.get(i);

        return nums;
    }
}
